using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;

namespace EntLibContrib.PolicyInjection
{
    /// <summary>
    /// Extends IMethodInvocation
    /// </summary>
    public static class MethodInvocationExtensions
    {
        private static readonly object InstanceInvocationContextKey = new object();

        /// <summary>
        /// Sets the Invocation Context that will be shared by all method invocations on a given instance.
        /// </summary>
        /// <param name="invocation">Current Invocation Context</param>
        /// <param name="instanceContext">Instance Invocation Context</param>
        public static void SetInstanceContext(IMethodInvocation invocation, IDictionary instanceContext)
        {
            invocation.InvocationContext[InstanceInvocationContextKey] = instanceContext;
        }

        /// <summary>
        /// Gets the current Instance Invocation Context.
        /// </summary>
        /// <param name="invocation">Invocation method</param>
        /// <returns>Instance Invocation context is present. null otherwise.</returns>
        public static IDictionary GetInstanceContext(IMethodInvocation invocation)
        {
            return (IDictionary)invocation.InvocationContext[InstanceInvocationContextKey];
        }
    }
}
